function pix2interp(himg)
% pix2interp(himg)
%
% syntax: Click on a map to make it active prior to calling,
% or pass an image handle as an input argument.
% Converts a pixel image to an interpolated one.
% To convert back to pixels, use interp2pix.
%
% To implement as a right-click option, use the following code:
% ----------------------------------------------------------------------------------------
% % pixel/interpolate option via uicontextmenu
% cmenu = uicontextmenu;
% set(himg, 'UIContextMenu', cmenu);
% % cb1 = ['pix2interp'];
% item1 = uimenu(cmenu, 'Label', 'activate pix/interp options', 'Callback', 'pix2interp');
% ----------------------------------------------------------------------------------------

% editing: gs 2003, 2004
% gs may 2005 -- private version for mapAverager1p0
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% use currently selected item if no handle passed
if nargin == 0
    if strcmp(get(gco, 'Type'), 'image')
        himg = gco;
    else
        beep;
        disp('Error in pix2interp: no image selected to operate on.');
        return
    end
end
        
% check that the active object is an image
if ~strcmp(get(himg, 'Type'), 'image')
    beep;
    disp('Error in pix2interp: no image selected to operate on.');
    return
end

% uicontextmenu
cmenu = uicontextmenu;
set(himg, 'UIContextMenu', cmenu);
% cb1 = ['pix2interp'];
item1 = uimenu(cmenu, 'Label', 'pixels', 'Callback', 'interp2pix', 'Checked', 'off');
item2 = uimenu(cmenu, 'Label', 'interpolated', 'Callback', 'pix2interp', 'Checked', 'on');

% set the lims (otherwise won't work with a plot
% whose axes lims were not set explicitly)
lims = get(gca, 'XLim'); 
set(gca, 'XLim', lims); 
lims = get(gca, 'YLim'); 
set(gca, 'YLim', lims); 

% set the density of interpolation
interpNum = 4;

% get the original map data
Mraw = get(himg, 'CData');
MrawSize = size(Mraw);
Mxdata = get(himg, 'XData');
Mydata = get(himg, 'YData');
M = Mraw;

% store the raw map in the image's UserData
set(himg, 'UserData', Mraw);

% % convert the inf's to numbers
% if max(max(M))==inf
%     diff = max(max(M(M~=inf))) - min(min(M));
%     M(M==inf) = max(max(M(M~=inf))) + 0.1*diff;
% end

% interpolate
Mint = interp2(M, interpNum);
MintSize = size(Mint);

% replace the original map with the interpolated one
set(himg, 'CData', Mint); % return

% 
set(gca, 'XTickLabel', '');
step = (max(Mxdata)-min(Mxdata))/(MrawSize(1)-1);
vec = min(Mxdata):step:max(Mxdata);
set(gca, 'XTick', vec);

set(gca, 'YTickLabel', '');
step = (max(Mydata)-min(Mydata))/(MrawSize(2)-1);
vec = min(Mydata):step:max(Mydata);
set(gca, 'YTick', vec);

interpRedOutForAvg(himg, 1);


% contour level could go here



% [X,Y] = meshgrid(X,Y);
% XI = 
% 
% ZI = interp2(X,Y,Z,XI,YI,'cubic')

